package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"os"
	"os/exec"
	"runtime"
	"strings"

	"github.com/thedevsaddam/gojsonq"
)

func posthook(writer http.ResponseWriter, request *http.Request) {

	// 检查是否POST请求
	if request.Method != "POST" {
		writer.WriteHeader(405)
		return
	}
	// 解析form
	err := request.ParseForm()
	if err != nil {
		writer.WriteHeader(400)
		return
	}
	// 设置Content-Type
	writer.Header().Set("Content-Type", "application/json")
	writer.WriteHeader(200)
	data := map[string]interface{}{
		"method": request.Method,
		"url":    request.RequestURI,
		"header": request.Header,
		// 包含URL里的查询参数
		"query": request.URL.Query(),
		// 只包含body里的参数
		"postForm": request.PostForm,
		// 包含URL和body里的参数
		"form": request.Form,
	}
	json.NewEncoder(writer).Encode(data)

	jq := gojsonq.New().Reader(request.Body)

	commit := jq.Find("commits")
	jq.Reset()
	git_name := jq.Find("repository.name")
	jq.Reset()
	message := jq.Find("commits.[0].message")
	jq.Reset()
	fmt.Println("------------------------------commit-----------------------------")
	fmt.Println(commit)
	fmt.Println("------------------------------message-----------------------------")
	fmt.Println(message)
	fmt.Println("------------------------------git_name-----------------------------")
	fmt.Println(git_name)
	file, err := os.Create("git_meaasge_" + git_name.(string) + ".txt") // 创建文件, "binbin"是文件名字
	if err != nil {
		fmt.Println("文件创建失败", err.Error())
	}
	defer file.Close()
	file.WriteString(message.(string))

	RunCommand("./" + "git_hook_" + git_name.(string) + ".sh")
	writer.Write([]byte("1111"))

}

func main() {
	http.HandleFunc("/", posthook)
	err := http.ListenAndServe(":7001", nil)
	if err != nil {
		fmt.Println("服务器启动失败", err.Error())
		return
	}

}

func runInLinux(cmd string) string {
	fmt.Println("Running Linux cmd:", cmd)
	result, err := exec.Command("/bin/sh", "-c", cmd).Output()
	if err != nil {
		fmt.Println(err.Error())
	}
	return strings.TrimSpace(string(result))
}

func runInWindows(cmd string) string {
	fmt.Println("Running Win cmd:", cmd)
	result, err := exec.Command("cmd", "/c", cmd).Output()
	if err != nil {
		fmt.Println(err.Error())
	}
	return strings.TrimSpace(string(result))
}

func RunCommand(cmd string) string {
	if runtime.GOOS == "windows" {
		return runInWindows(cmd)
	} else {
		return runInLinux(cmd)
	}
}

func RunLinuxCommand(cmd string) string {
	if runtime.GOOS == "windows" {
		return ""
	} else {
		return runInLinux(cmd)
	}
}

func runInLinuxWithErr(cmd string) (string, error) {
	fmt.Println("Running Linux cmd:" + cmd)
	result, err := exec.Command("/bin/sh", "-c", cmd).Output()
	if err != nil {
		fmt.Println(err.Error())
	}
	return strings.TrimSpace(string(result)), err
}

func runInWindowsWithErr(cmd string) (string, error) {
	fmt.Println("Running Windows cmd:" + cmd)
	result, err := exec.Command("cmd", "/c", cmd).Output()
	if err != nil {
		fmt.Println(err.Error())
	}
	return strings.TrimSpace(string(result)), err
}

func RunCommandWithErr(cmd string) (string, error) {
	if runtime.GOOS == "windows" {
		return runInWindowsWithErr(cmd)
	} else {
		return runInLinuxWithErr(cmd)
	}
}

func RunLinuxCommandWithErr(cmd string) (string, error) {
	if runtime.GOOS == "windows" {
		return "", errors.New("could not run in Windows Os")
	} else {
		return runInLinuxWithErr(cmd)
	}
}
